package logic

import (
	"context"

	"mall/app/product/cmd/rpc/internal/svc"
	"mall/app/product/cmd/rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetCategoryListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetCategoryListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCategoryListLogic {
	return &GetCategoryListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetCategoryList 获取全部商品分类
func (l *GetCategoryListLogic) GetCategoryList(in *pb.EmptyReq) (*pb.GetCategoryListRes, error) {
	list, err := l.svcCtx.CategoryModel.FindAll(l.ctx)
	if err != nil {
		return nil, err
	}
	res := &pb.GetCategoryListRes{}
	for _, v := range list {
		res.List = append(res.List, &pb.CategoryEntity{
			CatId:        v.CatId,
			Name:         v.Name,
			ParentCid:    v.ParentCid,
			CatLevel:     v.CatLevel,
			ShowStatus:   v.ShowStatus,
			Sort:         v.Sort,
			Icon:         v.Icon,
			ProductUnit:  "",
			ProductCount: v.ProductCount,
		})
	}
	return res, nil
}
